package com.younion.task;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.younion.business.ICardEntityService;
import com.younion.business.ICustomerService;
import com.younion.business.IMemberCardService;
import com.younion.business.IMemberCardUpgradeLogService;
import com.younion.business.IUpgradeRuleService;
import com.younion.vo.CardEntityVO;
import com.younion.vo.CustomerVO;
import com.younion.vo.MemberCardUpgradeLogVO;
import com.younion.vo.MemberCardVO;
import com.younion.vo.UpgradeRuleVO;

public class AutoUpgradeMemberCardTask extends Task {
	
	private static Logger logger = LoggerFactory.getLogger(AutoUpgradeMemberCardTask.class);
	
	private IMemberCardService memberCardService = null;
	
	private ICardEntityService cardEntityService = null;
	@Autowired
	private ICustomerService customerService = null;
	@Autowired
	private IUpgradeRuleService upgradeRuleService = null;
	@Autowired
	private IMemberCardUpgradeLogService memberCardUpgradeLogService = null;
	
	@Override
	public void work() {
		MemberCardVO mc = null;//会员卡实例
		CardEntityVO ce = null;//会员卡实体
		UpgradeRuleVO ur = null;//升级规则
		CustomerVO c = null;//商家
		
		//定义一个专用于更新的VO避免更新不必要的字段
		MemberCardVO mcTemp = null;
		CardEntityVO newCE = null;
		try {
			List<MemberCardVO> list = (List<MemberCardVO>) this.memberCardService.getAll();
			for (int i = 0; i < list.size(); i++) {
				mc = list.get(i);
				ur = this.upgradeRuleService.getByOldCardId(mc.getCardId());
				if (ur != null) {
					if (mc.getScore() >= ur.getNeedPoint()) {
						mc.setScore(mc.getScore() - ur.getDeductionPoint());
						mcTemp = new MemberCardVO();
						mcTemp.setId(mc.getId());
						mcTemp.setCardId(ur.getNewCardEntityId());//升级为新卡
						mcTemp.setScore(mc.getScore());//新积分
						this.memberCardService.save(mcTemp);
						ce = this.cardEntityService.getById(String.valueOf(mc.getCardId()));
						c = this.customerService.getById(String.valueOf(mc.getCustomerId()));
						newCE = this.cardEntityService.getById(String.valueOf(ur.getNewCardEntityId()));//获取新卡
						//记录升级日志
						this.saveUpgradeLog(c, mc, ce, newCE);
					}
				}
			}
		} catch (Exception ex) {
			logger.error("AutoUpgradeMemberCardTask >>> Task Error");
			logger.error(ex.toString());
		}
	}
	
	//保存升级日志
	private void saveUpgradeLog(CustomerVO c, MemberCardVO mc, CardEntityVO oldCE, CardEntityVO newCE) {
		String temp = "商家【"+c.getName()+"】的会员卡【"+mc.getSchemeCode()+"："+(mc.getMemberName()==null?"":mc.getMemberName())+"】由【级别为：["+oldCE.getCardLevelVO().getLevelName()+"："+oldCE.getCardLevelVO().getLevel()+"]的会员卡："+oldCE.getCardName()+"】更换为【级别为：["+newCE.getCardLevelVO().getLevelName()+"："+newCE.getCardLevelVO().getLevel()+"]的会员卡："+newCE.getCardName()+"】";
		MemberCardUpgradeLogVO mcul = new MemberCardUpgradeLogVO();
		mcul.setMemberCardId(mc.getId());
		mcul.setCustomerId(mc.getCustomerId());
		mcul.setUserId(mc.getUserId());
		mcul.setOldCardId(oldCE.getId());
		mcul.setNewCardId(newCE.getId());
		mcul.setOperUserId(null);
		mcul.setRemark(temp);
		try {
			this.memberCardUpgradeLogService.insertByVo(mcul);
		} catch (Exception ex) {
			logger.error("AutoUpgradeMemberCardTask >>> saveUpgradeLog Error");
			logger.error(ex.toString());
		}
	}

	public void setCardEntityService(ICardEntityService cardEntityService) {
		this.cardEntityService = cardEntityService;
	}

	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	public void setMemberCardService(IMemberCardService memberCardService) {
		this.memberCardService = memberCardService;
	}

	public void setUpgradeRuleService(IUpgradeRuleService upgradeRuleService) {
		this.upgradeRuleService = upgradeRuleService;
	}

	public void setMemberCardUpgradeLogService(
			IMemberCardUpgradeLogService memberCardUpgradeLogService) {
		this.memberCardUpgradeLogService = memberCardUpgradeLogService;
	}

}
